<template>
  <div class="front-container">
    <!-- 顶部导航栏 -->
    <header class="header-nav">
      <div class="header-left-warp">
        <div class="logo-warp">
          <div class="logo">
            <img src="../../config/logo.svg" alt="Logo" />
          </div>
          <div class="logo-text">{{ projectName }}</div>
        </div>

        <div class="header-navs">
          <el-menu router :default-active="activeMenu" mode="horizontal" :ellipsis="false">
            <!--前台路由-->
            <el-menu-item index="/front/home">前台首页</el-menu-item>
            <!--前台路由-->
          </el-menu>
        </div>
      </div>

      <div class="user-warp">
        <!-- 未登录状态显示登录注册按钮 -->
        <template v-if="!account.id">
          <div class="btn-login">
            <el-button @click="router.push('/login')">登录</el-button>
          </div>
          <div class="btn-login" style="margin-left: 10px">
            <el-button @click="router.push('/register')">注册</el-button>
          </div>
        </template>

        <!-- 已登录状态显示用户头像和下拉菜单 -->
        <el-dropdown v-else class="custom-dropdown">
          <div class="user-avatar">
            <img :src="account.avatarUrl" />
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>{{ account.nickname }}</el-dropdown-item>
              <el-dropdown-item>
                <router-link to="/front/person" class="dropdown-link">
                  <el-icon>
                    <User />
                  </el-icon>
                  <span>个人信息</span>
                </router-link>
              </el-dropdown-item>
              <el-dropdown-item>
                <router-link to="/front/password" class="dropdown-link">
                  <el-icon>
                    <Lock />
                  </el-icon>
                  <span>修改密码</span>
                </router-link>
              </el-dropdown-item>
              <el-dropdown-item>
                <div @click="logout" class="dropdown-link">
                  <el-icon>
                    <SwitchButton />
                  </el-icon>
                  <span>退出登录</span>
                </div>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>

    <!-- 主内容区域 -->
    <div class="main-content">
      <router-view @update-account="handleUpdateAccount"></router-view>
    </div>

    <!-- 页脚 -->
    <footer class="front-footer">
      <p>© {{ new Date().getFullYear() }} {{ projectName }}. 保留所有权利</p>
    </footer>
  </div>
</template>
<script>
import { Lock, SwitchButton, User } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { projectName } from '../../config/config.default'

export default {
  components: {
    User,
    Lock,
    SwitchButton
  },
  data() {
    return {
      projectName: projectName,
      account: localStorage.getItem('account')
        ? JSON.parse(localStorage.getItem('account'))
        : {}
    }
  },
  computed: {
    activeMenu() {
      return this.$route.path
    }
  },
  methods: {
    logout() {
      localStorage.removeItem('account')
      ElMessage.success('退出成功')
      this.$router.push('/login')
    },
    handleUpdateAccount(updatedAccount) {
      this.account = updatedAccount
    }
  }
}
</script>

<style>
:root {
  --front-font-color: #4084d9;
  --front-back-color: #fff;
  --footer-height: 50px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Helvetica Neue', Arial, sans-serif;
}

body,
html {
  height: 100%;
  background-color: #f5f7fa;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header-nav {
  z-index: 1800;
  position: sticky;
  top: 0;
  height: 70px;
  background-color: var(--front-back-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 40px;
  box-shadow: 0 2px 10px 0 rgba(0, 0, 0, 0.1);
}

.header-left-warp {
  display: flex;
  align-items: center;
  height: 100%;
}

.logo-warp {
  display: flex;
  align-items: center;
  margin-left: 20px;
}

.logo {
  width: 30px;
  height: 30px;
  margin-right: 10px;
}

.logo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.logo-text {
  font-size: 22px;
  font-weight: 500;
  color: var(--front-font-color);
}

.header-navs {
  margin-left: 80px;
  height: 100%;
}

.el-menu {
  background-color: var(--front-back-color) !important;
  border: none !important;
  height: 70px !important;
}

.el-menu-item {
  height: 70px !important;
  line-height: 70px !important;
  font-size: 16px;
}

.el-menu-item:hover {
  color: var(--front-font-color) !important;
  background-color: transparent !important;
}

.el-menu-item.is-active {
  color: var(--front-font-color) !important;
  background-color: transparent !important;
  border: none !important;
}

.user-warp {
  display: flex;
  align-items: center;
  margin-right: 20px;
  height: 100%;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  border: 1px solid var(--front-font-color);
  padding: 2px;
  cursor: pointer;
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.dropdown-link {
  display: flex;
  align-items: center;
  color: inherit;
  text-decoration: none;
  width: 100%;
  padding: 5px 0;
}

.el-icon {
  margin-right: 8px;
}

/* 修复页脚位置的关键代码 */
.main-content {
  flex: 1;
  padding: 20px 40px;
  background-color: #fff;
  min-height: calc(100vh - 70px - var(--footer-height));
}

.front-footer {
  height: var(--footer-height);
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
  color: #666;
  font-size: 14px;
  border-top: 1px solid #eee;
}

/* 示例内容样式 */
.content-section {
  margin: 30px 0;
}

.content-title {
  font-size: 24px;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid var(--front-font-color);
}

.card-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.card {
  border: 1px solid #ebeef5;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s, box-shadow 0.3s;
  background-color: #fff;
}

.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.1);
}

.card-img {
  height: 180px;
  overflow: hidden;
}

.card-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s;
}

.card:hover .card-img img {
  transform: scale(1.05);
}

.card-content {
  padding: 20px;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 10px;
  color: #333;
}

.card-desc {
  color: #666;
  line-height: 1.6;
  margin-bottom: 15px;
  font-size: 14px;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid #f0f2f5;
  padding-top: 15px;
}

.btn-login {
  margin-left: 10px;
}

@media (max-width: 768px) {
  .header-nav {
    padding: 0 20px;
  }

  .header-navs {
    margin-left: 20px;
  }

  .logo-text {
    display: none;
  }

  .card-container {
    grid-template-columns: 1fr;
  }

  .main-content {
    padding: 15px;
  }
}
</style>
